<template>
<div class="header">
  <el-container>
    <el-header>
      <el-row>
        <el-col class="logo" :span="3">
          <img src="@/assets/head-logo.svg" alt=""/>
        </el-col>
        <el-col class="nav" :span="16">
          <el-row>
            <!-- 加入current 实现高亮效果 -->
            <el-col v-for="nav in nav_list" :span="3" :key="nav.id">
              <a :class="check(nav.link) ? 'current': ''" :href="nav.link">{{nav.name}}</a>
            </el-col>
<!--            <el-col :span="3"><router-link class="current" to="/home">免费课</router-link></el-col>-->
<!--            <el-col :span="3"><router-link to="/">轻课</router-link></el-col>-->
<!--            <el-col :span="3"><router-link to="/">学位课</router-link></el-col>-->
<!--            <el-col :span="3"><router-link to="/">题库</router-link></el-col>-->
<!--            <el-col :span="3"><router-link to="/">教育</router-link></el-col>-->
          </el-row>
        </el-col>
        <el-col class="login-bar" :span="5">
          <el-row v-if="token">
            <el-col class="cart-ico" :span="9">
              <router-link to="">
              <b class="goods-number">{{this.$store.state.cart.count}}</b>
              <img class="cart-icon" src="@/assets/download.svg" alt=""/>
             <span><router-link to="/cart">购物车</router-link></span>
              </router-link>
            </el-col>
            <el-col class="study" :span="8" :offset="2"><router-link to="">学习中心</router-link></el-col>
            <el-col class="member" :span="5">
              <el-menu class="el-menu-demo" mode="horizontal">
                <el-submenu index="2">
                  <template slot="title"><router-link to=""><img src="@/assets/logo@2x.png" alt=""/></router-link></template>
                  <el-menu-item index="2-1"><a href="/my">我的账户</a></el-menu-item>
                  <el-menu-item index="2-2">我的订单</el-menu-item>
                  <el-menu-item index="2-3">我的优惠券</el-menu-item>
                  <el-menu-item index="2-4"><span @click="logout">退出登录</span></el-menu-item>
                </el-submenu>
              </el-menu>
            </el-col>
          </el-row>
           <el-row v-else>
            <el-col class="cart-ico" :span="9">
              <router-link to="">
              <b class="goods-number">0</b>
              <img class="cart-icon" src="@/assets/download.svg" alt=""/>
               <span><router-link to="/cart">购物车</router-link></span>
              </router-link>
            </el-col>
            <el-col class="member" :span="12" offset="3">
              <span class="register">
                <router-link to="/login">登录</router-link>
                &nbsp;&nbsp;|&nbsp;&nbsp;
                <router-link to="/register">注册</router-link>
              </span>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-header>
  </el-container>
</div>
</template>

<script>
  export default {
    name: "Header",
    data(){
      return {
        // 设置一个flag标识，用于标识登录状态
        token: sessionStorage.token || localStorage.token,
        user_name: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.id || localStorage.id,
        nav_list: [],
        cart_count: 0,
      };
    },
    created(){
      // 获取购物车内商品数量值
      this.cart_count = this.$store.state.cart.count || 0;
      // 获取导航栏信息
      this.$axios.get(this.$settings.Host + '/nav/').then(response=>{
        console.log(response.data);
        this.nav_list = response.data;
      }).catch(error=>{
        console.log(error.response);
      })
    },
    methods: {
      // 实现导航高亮
      check(link){
        return link == window.location.pathname
      },

      // logout
      logout(){
        this.token = false;
        this.user_id = false;
        this.user_name = false;

        sessionStorage.removeItem('token');
        sessionStorage.removeItem('id');
        sessionStorage.removeItem('username');

        localStorage.removeItem('token');
        localStorage.removeItem('id');
        localStorage.removeItem('username');

        this.$alert('退出登录成功', '路飞学城', {
          confirmButtonText: '确定'
        });
      },
    },
  }
</script>

<style scoped>
  .header{
    top: 0;
    left: 0;
    right: 0;
    margin: auto;
    background-color: #ffffff;
    height: 80px;
    z-index: 1000;
    position: fixed;
    width: 100%;
    box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
  }
  .header .el-container{
    width: 1200px;
    margin: 0 auto;
  }
  .el-header{
    height: 80px!important;
    padding: 0;
  }
  .logo img{
    padding-top: 17px;
  }
  .nav{
    margin-top: 22px;
  }
  .nav .el-col a{
    display: block;
    text-align: center;
    margin-left: 30px;
    padding-bottom: 16px;
    padding-left: 5px;
    padding-right: 5px;
    position: relative;
    font-size: 16px;
    text-decoration: none;
  }
  /* 加入current实现高亮效果 */
  .nav .el-col .current{
    color: #4a4a4a;
    border-bottom: 4px solid #ffc210;
  }
  .login-bar{
    margin-top: 22px;
  }
  .cart-ico{
    position: relative;
    border-radius: 17px;
  }
  .cart-ico:hover{
    background: #f0f0f0;
  }
  .goods-number{
    width: 16px;
    height: 16px;
    line-height: 17px;
    font-size: 12px;
    color: #ffffff;
    text-align: center;
    background: #fa6240;
    border-radius: 50%;
    transform: scale(.8);
    position: absolute;
    left: 16px;
    top: -1px;
  }
  .cart-icon{
    width: 15px;
    height: auto;
    margin-left: 6px;
  }
  .cart-ico span{
    margin-left: 12px;
  }
  .cart-ico a{
    text-decoration: none;
  }
  .member img{
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: inline-block;
  }
  .member img:hover{
    border: 1px solid yellow;
  }
  .study a{
    text-decoration: none;
  }
</style>
